//
//  HeTongDetailVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/7/3.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "HeTongDetailVC.h"
#import "RejectOperationView.h"
#import "LJInPutTool.h"
#import "HeTongManagerDetailCell.h"
#import "BaoJiaBillVC.h"

@interface HeTongDetailVC () <UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) RejectOperationView *topView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) LJInPutTool *inPutTool;//输入框
@property (nonatomic, strong) HeTongManagerModel *model;

@end

@implementation HeTongDetailVC

-(HeTongManagerModel *)model {
    if (!_model) {
        _model = [HeTongManagerModel new];
    }
    return _model;
}

-(LJInPutTool *)inPutTool {
    if (!_inPutTool) {
        _inPutTool = [LJInPutTool new];
        _inPutTool.placeholder = @"请输入驳回原因";
        _inPutTool.sureBtnText = @"确认驳回";
        _inPutTool.cancleBtnText = @"取消驳回";
        __weak_self;
        _inPutTool.finishBlock = ^(NSString * _Nonnull str) {
            [weakself boHuiHeTong:str];
        };
    }
    return _inPutTool;
}

- (void)boHuiHeTong:(NSString *)str {
    [HttpRequest PostWithApi:ReceiptDeliveryHeTongManagerBoHui params:@{@"constract_sn":self.model.constract_sn,@"reject_remarks":str} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"已驳回"];
            [self requestForData];
            [[NSNotificationCenter defaultCenter] postNotificationName:ReceiptDeliveryRefreshHeTongManagerList object:nil];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

-(RejectOperationView *)topView {
    if (!_topView) {
        _topView = [RejectOperationView new];
        _topView.canOperation = NO;
        _topView.frame = CGRectMake(0, 0, ScreenW, self.topView.height);
    }
    return _topView;
}

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - NaviHeight - 64, ScreenW, 64+BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
        CGFloat btnW = (ScreenW - 30 - 30 - 15) * 0.5;
        CGFloat btnH = 44;
        UIView *bg1View = [[UIView alloc] initWithFrame:CGRectMake(30, 10, btnW, btnH)];
        bg1View.backgroundColor = HexColor(0xF35959);
        bg1View.layer.masksToBounds = YES;
        bg1View.layer.cornerRadius = 6;
        [_bottomView addSubview:bg1View];
        
        UIView *bg2View = [[UIView alloc] initWithFrame:CGRectMake(bg1View.nim_right + 15, 10, btnW, btnH)];
        bg2View.backgroundColor = Color_ThemeColor;
        bg2View.layer.masksToBounds = YES;
        bg2View.layer.cornerRadius = 6;
        [_bottomView addSubview:bg2View];
        
        UIImageView *arrowImgView = [UIImageView new];
        [arrowImgView setImage:[UIImage imageNamed:@"icon_reject"]];
        [bg1View addSubview:arrowImgView];
        
        UILabel *arrowLab = [UILabel new];
        arrowLab.textColor = HexColor(0xffffff);
        arrowLab.font = BoldFontOfSize(16);
        arrowLab.text = @"驳回";
        [bg1View addSubview:arrowLab];
        [arrowLab sizeToFit];
        arrowImgView.nim_width = 18;
        arrowImgView.nim_height = 18;
        arrowImgView.nim_left = (bg1View.nim_width - 10 - arrowImgView.nim_width - arrowLab.nim_width) * 0.5;
        arrowImgView.nim_centerY = bg1View.nim_height * 0.5;
        
        arrowLab.nim_left = arrowImgView.nim_right + 10;
        arrowLab.nim_centerY = arrowImgView.nim_centerY;
        
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, bg1View.nim_width, bg1View.nim_height)];
        [btn addTarget:self action:@selector(rejectClick) forControlEvents:UIControlEventTouchUpInside];
        [bg1View addSubview:btn];
        
        UIImageView *arrow2ImgView = [UIImageView new];
        [arrow2ImgView setImage:[UIImage imageNamed:@"icon_adopt"]];
        [bg2View addSubview:arrow2ImgView];
        
        UILabel *arrow2Lab = [UILabel new];
        arrow2Lab.textColor = HexColor(0xffffff);
        arrow2Lab.font = BoldFontOfSize(16);
        arrow2Lab.text = @"通过";
        [bg2View addSubview:arrow2Lab];
        [arrow2Lab sizeToFit];
        arrow2ImgView.nim_width = 18;
        arrow2ImgView.nim_height = 18;
        arrow2ImgView.nim_left = (bg2View.nim_width - 10 - arrow2ImgView.nim_width - arrow2Lab.nim_width) * 0.5;
        arrow2ImgView.nim_centerY = bg2View.nim_height * 0.5;
        
        arrow2Lab.nim_left = arrow2ImgView.nim_right + 10;
        arrow2Lab.nim_centerY = arrow2ImgView.nim_centerY;
        
        UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, bg2View.nim_width, bg2View.nim_height)];
        [btn2 addTarget:self action:@selector(passClick) forControlEvents:UIControlEventTouchUpInside];
        [bg2View addSubview:btn2];
    }
    return _bottomView;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - BottomHeight)];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"img_occupy"
             titleStr:@""
            detailStr:@""];
    }
    return _tableView;
}

- (void)rejectClick {
    [self.inPutTool showInView:MAIN_WINDOW];
}

- (void)passClick {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确定通过合同吗？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self requestForPass];
    }];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:sureAction];
    [alert addAction:cancleAction];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)requestForPass {
    [HttpRequest PostWithApi:ReceiptDeliveryHeTongManagerPass params:@{@"constract_sn":self.model.constract_sn} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"已通过"];
            [self requestForData];
            [[NSNotificationCenter defaultCenter] postNotificationName:ReceiptDeliveryRefreshHeTongManagerList object:nil];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"合同详情";
    [self.view addSubview:self.topView];
    [self.view addSubview:self.bottomView];
    [self.view addSubview:self.tableView];
    [self requestForData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HeTongManagerDetailCell *cell = [HeTongManagerDetailCell HeTongManagerDetailCellWithTableView:tableView];
    cell.model = self.model;
    __weak_self;
    cell.lookClock = ^{
        BaoJiaBillVC *vc = [BaoJiaBillVC new];
        vc.q_id = weakself.model.q_id;
        [weakself.rt_navigationController pushViewController:vc animated:YES];
    };
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    HeTongManagerDetailCell *cell = [HeTongManagerDetailCell new];
    return cell.height;
}

- (void)requestForData {
    [HttpRequest PostWithApi:ReceiptDeliveryHeTongManagerDetail params:@{@"co_id":self.co_id} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            self.model = [HeTongManagerModel mj_objectWithKeyValues:responseObject];
            if (self.model.status == HeTongManagerModelStatusWaitSure) {
                [self.view addSubview:self.bottomView];
                [self.topView removeFromSuperview];
                self.tableView.frame = CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - self.bottomView.nim_height);
            }else if (self.model.status == HeTongManagerModelStatusReject) {
                [self.view addSubview:self.topView];
                [self.bottomView removeFromSuperview];
                self.tableView.frame = CGRectMake(0, self.topView.nim_bottom, ScreenW, ScreenH - NaviHeight - BottomHeight - self.topView.nim_height);
                self.topView.titleStr = @"合同审核未通过！ ";
                self.topView.timeStr = self.model.rejectTime;
                self.topView.descStr = self.model.reject_remarks;
            }else {
                [self.topView removeFromSuperview];
                [self.bottomView removeFromSuperview];
                self.tableView.frame = CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - BottomHeight);
            }
            [self.tableView reloadData];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

@end
